package com.superman.supermarket.client.activity;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.event.ReloadEvent;
import com.superman.mvpframe.client.mvp.IPageView;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.mvpframe.client.widget.mescroll.UpAndDownCallBack;
import com.superman.mvpframe.client.widget.tabbar.MyTabBarSelectEvent;
import com.superman.mvpframe.client.widget.tabbar.MyTabBarSelectEvent.MyTabBarSelectHandler;
import com.superman.supermarket.client.constant.OrderListTabConstants;
import com.superman.supermarket.client.mvp.ShopAbstractActivity;
import com.superman.supermarket.client.view.orderlist.OrderListView;

/**
 * 订单列表
 * 
 * @author superman
 * @version 2018年8月2日下午5:22:03
 */
public class OrderListActivity extends ShopAbstractActivity implements UpAndDownCallBack {

	@Inject
	OrderListView orderListView;

	private int state = OrderListTabConstants.ALL.getState();

	@Override
	public void getListDataByPage(int page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("PAGE_INDEX", page);
		map.put("FILTER", state);
		postman.post("order/list2", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onBusinessOK(String response) {
				orderListView.getPullToRefreshView().endBySize(XJsonUtil.getList(response), XJsonUtil.getCount(response));
			}
		});
	}

	@Override
	public IPageView getView() {
		return orderListView;
	}

	@Override
	public IPageViewPresenter getViewPresenter() {
		return null;
	}

	private void initTabbar() {
		int tabIndex = OrderListTabConstants.ALL.getTabIndex();
		if (Window.Location.getParameter("tabindex") != null)
			tabIndex = Integer.parseInt(Window.Location.getParameter("tabindex"));
		orderListView.getTabBar().addMyTabBarSelectHandler(new MyTabBarSelectHandler() {

			@Override
			public void onTabBarSelected(MyTabBarSelectEvent event) {
				int selectedIndx = event.getIndex();
				for (int i = 0; i < OrderListTabConstants.values().length; i++) {
					OrderListTabConstants oc = OrderListTabConstants.values()[i];
					if (selectedIndx == oc.getTabIndex()) {
						OrderListActivity.this.state = oc.getState();
						orderListView.getPullToRefreshView().clearListAndResetScroll();
						break;
					}
				}
			}
		});
		orderListView.getTabBar().select(tabIndex, false);
	}

	@Override
	public void onActivity(EventBus eventBus) {
		eventBus.addHandler(ReloadEvent.TYPE, new ReloadEvent.ReloadEventHandler() {

			@Override
			public void reload(ReloadEvent event) {
				orderListView.getPullToRefreshView().clearListAndResetScroll();
			}
		});
	}

	@Override
	public void popToRefresh() {

	}

	@Override
	public void popToRefresh(Object contextData) {

		if (contextData instanceof Map) {
			Map map = (Map) contextData;
			if (map.containsKey("orderid"))
				orderListView.getPullToRefreshView().clearListAndResetScroll();
		}

	}

	@Override
	public void pushToRefresh() {
		if (Window.Location.getParameter("state") != null)
			this.state = Integer.parseInt(Window.Location.getParameter("state"));
		orderListView.getPullToRefreshView().initMeScroll(this, "2.533333rem", "0rem");

		initTabbar();
	}

}
